/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sunsite.action;

import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.support.ResourceBundleMessageSource;
import sunsite.po.News;
import sunsite.po.Resource;
import sunsite.service.NewsService;
import sunsite.service.ResourceService;
import sunsite.service.VisitService;
import sunsite.tools.ContextHelper;

/**
 *
 * @author mg
 */
public class Index extends ActionSupport {

    public static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(Register.class);
    private List<News> top5News;
    private List<Resource> top10Software;
    private List<Resource> latestResource;

    public List<Resource> getLatestResource() {
        return latestResource;
    }

    public void setLatestResource(List<Resource> latestResource) {
        this.latestResource = latestResource;
    }
    private NewsService newsService;
    private ResourceService resourceService;
    private VisitService visitService;

    public void setVisitService(VisitService visitService) {
        this.visitService = visitService;
    }

    public void setResourceService(ResourceService resourceService) {
        this.resourceService = resourceService;
    }

    public void setMessageSource(ResourceBundleMessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public void setNewsService(NewsService newsService) {
        this.newsService = newsService;
    }
    private ResourceBundleMessageSource messageSource;

    @Override
    public String execute() throws Exception {
        if (ContextHelper.getApplication().get("top5News") == null && ContextHelper.getApplication().get("top10Software") == null && ContextHelper.getApplication().get("latestResource") == null) {
            top5News = newsService.getTop5News();
            top10Software = resourceService.getTop10Software();
            latestResource = resourceService.getLatestResource();
            ContextHelper.getApplication().put("top5News", top5News);
            ContextHelper.getApplication().put("top10Software", top10Software);
            ContextHelper.getApplication().put("latestResource", latestResource);
        }else{
            top5News = (List<News>)ContextHelper.getApplication().get("top5News");
            top10Software = (List<Resource>)ContextHelper.getApplication().get("top10Software");
            latestResource = (List<Resource>)ContextHelper.getApplication().get("latestResource");
        }
        String requestIp = ServletActionContext.getRequest().getRemoteAddr();
        Date visitTime = new Date();
        boolean isFirstTime = ContextHelper.getSession().get(sunsite.tools.ParamsString.VisitId) == null ? true : false;
        if (isFirstTime) {
            String visitId = visitService.logVisit(visitTime, requestIp);
            ContextHelper.getSession().put(sunsite.tools.ParamsString.VisitId, visitId);
        }
        return SUCCESS;
    }

    public List<News> getTop5News() {
        return top5News;
    }

    public void setTop5News(List<News> top5News) {
        this.top5News = top5News;
    }

    public List<Resource> getTop10Software() {
        return top10Software;
    }

    public void setTop10Software(List<Resource> top10Software) {
        this.top10Software = top10Software;
    }
}
